package com.pshcp.api;

import com.external.common.WebServiceException;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/2 10:00
 */
@Slf4j
@ControllerAdvice
public class BaseExceptionHandlerAPI {

    /**
     * 处理自定义业务异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = {WebServiceException.class})
    @ResponseBody
    public ResultDTO<String> errorWebServiceRetFailedException(WebServiceException e) {
        log.error(e.getMessage(), e);
        return ResultBuilder.failure(e.getMessage(), e.getMessage());
    }

    /**
     * 处理参数验证异常
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value ={ MethodArgumentNotValidException.class})
    @ResponseBody
    public Object errorException(MethodArgumentNotValidException e) {
        StringBuilder errMsg = new StringBuilder();
        e.getBindingResult().getAllErrors().stream().forEach(item -> {
            errMsg.append(item.getDefaultMessage());
        });
        log.error(errMsg.toString());
        return ResultBuilder.failure("90000012", errMsg.toString());
    }

    /**
     * 处理异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
    @ResponseBody
    public ResultDTO<String>  errorHttpMessageNotReadableException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultBuilder.failure("90000005");
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(value = {NoHandlerFoundException.class})
    @ResponseBody
    public ResultDTO<String>  noHandlerFoundException(HttpServletRequest request, NoHandlerFoundException e) throws BizException {
        log.error(e.getMessage(), e);
        return ResultBuilder.failure("90000001");
    }

    /**
     * 处理自定义业务异常
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = {BizException.class})
    @ResponseBody
    public ResultDTO<String> errorBizException(BizException e) {
        log.error(e.getMessage(), e);
        return ResultBuilder.failure(e);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public ResultDTO<String>  errorException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultBuilder.failure("90000000");
    }

}
